package com.yitong.base.util;

import java.net.URLDecoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * 
 * 
 *
 */
public class CookieUtis {


	// 查询本地 是否有Cookie
	/*public static User returnCookies(HttpServletRequest request,UserService userService) {
		 User user = (User) request.getSession().getAttribute("user");
		 if(user!=null){
			 return user;
		 }else{
			 Cookie[] cookie =  request.getCookies();
			 for (int i = 0; i < cookie.length; i++) {
				 if(cookie[i].getName().equals("kuaiditu_name")){
					 User u = userService.getUser(cookie[i].getValue());
					 if(u!=null){
						 request.getSession().setAttribute("user", u);
					 }
					 return u;
				 }
			 }
			 return user;
		 }
	}*/
	//循环判断 寄件人记录中cookies（电话，地址，姓名） 获取所有的cookies 值 
	public static String returnRecordCookies(HttpServletRequest request,String recordkuaidituWeb)  {
		String str = "";
		Cookie[] cookie = request.getCookies();
		if (cookie != null) {
			for (int i = 0; i < cookie.length; i++) {
				if (cookie[i].getName().equals(recordkuaidituWeb)) {
					str = cookie[i].getValue();
				}
			}
		}
		return str;
	}
	//添加cookies
	public static void returnAddCookies(String recordkuaidituWeb,HttpServletResponse response,String str)  {
		//重新添加cookies
		Cookie cookieAddress = new Cookie(recordkuaidituWeb,str);
		cookieAddress.setMaxAge(60 * 60 * 24 * 150);
		cookieAddress.setPath("/");
		response.addCookie(cookieAddress);
	}
	//清空 cookies
	public static void retunCleanCookies(String recordkuaidituWeb,HttpServletResponse response){
		//清空cookies
		Cookie newCookie = new Cookie(recordkuaidituWeb, null); 
		newCookie.setMaxAge(0); 
		newCookie.setPath("/"); 
		response.addCookie(newCookie);
	}
	//记录电话 地址 姓名 cookie
	public static void RecordVoidCookie(HttpServletRequest request,HttpServletResponse response,String recordAddress,String kuaidituWebAddress_1,
			String kuaidituWebAddress_2,String kuaidituWebAddress_3){
		
		String Address_1 =  CookieUtis.returnRecordCookies(request, kuaidituWebAddress_1);
		String Address_2 =  CookieUtis.returnRecordCookies(request, kuaidituWebAddress_2);
		String Address_3 =  CookieUtis.returnRecordCookies(request, kuaidituWebAddress_3);
		if(!"".equals(Address_1)&&!"".equals(Address_2)&&!"".equals(Address_3)){
			if(!recordAddress.equals(Address_1)&&!recordAddress.equals(Address_2)&&!recordAddress.equals(Address_3)){
				CookieUtis.retunCleanCookies(kuaidituWebAddress_1, response);
				CookieUtis.retunCleanCookies(kuaidituWebAddress_2, response);
				CookieUtis.retunCleanCookies(kuaidituWebAddress_3, response);
				CookieUtis.returnAddCookies(kuaidituWebAddress_1, response, recordAddress);
				CookieUtis.returnAddCookies(kuaidituWebAddress_2, response, Address_1);
				CookieUtis.returnAddCookies(kuaidituWebAddress_3, response, Address_2);
			}
			if(recordAddress.equals(Address_1)){
				CookieUtis.retunCleanCookies(kuaidituWebAddress_1, response);
				CookieUtis.returnAddCookies(kuaidituWebAddress_1, response, recordAddress);
			}else if(recordAddress.equals(Address_2)){
				CookieUtis.retunCleanCookies(kuaidituWebAddress_2, response);
				CookieUtis.returnAddCookies(kuaidituWebAddress_2, response, recordAddress);
			}else if(recordAddress.equals(Address_3)){
				CookieUtis.retunCleanCookies(kuaidituWebAddress_3, response);
				CookieUtis.returnAddCookies(kuaidituWebAddress_3, response, recordAddress);
			}else{}
		}else{
			if(recordAddress.equals(Address_1)){
				CookieUtis.retunCleanCookies(kuaidituWebAddress_1, response);
				CookieUtis.returnAddCookies(kuaidituWebAddress_1, response, recordAddress);
			}else if(recordAddress.equals(Address_2)){
				CookieUtis.retunCleanCookies(kuaidituWebAddress_2, response);
				CookieUtis.returnAddCookies(kuaidituWebAddress_2, response, recordAddress);
			}else if(recordAddress.equals(Address_3)){
				CookieUtis.retunCleanCookies(kuaidituWebAddress_3, response);
				CookieUtis.returnAddCookies(kuaidituWebAddress_3, response, recordAddress);
			}else{}
			if("".equals(Address_1)){
				CookieUtis.returnAddCookies(kuaidituWebAddress_1, response, recordAddress);
			}else if("".equals(Address_2)){
				CookieUtis.returnAddCookies(kuaidituWebAddress_2, response, recordAddress);
			}else if("".equals(Address_3)){
				CookieUtis.returnAddCookies(kuaidituWebAddress_3, response, recordAddress);
			}else{}
		}
	}
	//查询cookie 根据cookie Key
	@SuppressWarnings("deprecation")
	public static String returnStringCookie(HttpServletRequest request,String kuaidituWebAddress_1,String kuaidituWebAddress_2,String kuaidituWebAddress_3) throws Exception{
		String str = "";
		Cookie[] cookie = request.getCookies();
		if (cookie != null) {
			for (int i = 0; i < cookie.length; i++) {
				if (cookie[i].getName().equals(kuaidituWebAddress_1)) {
					str += URLDecoder.decode(cookie[i].getValue())+",";
				}
				if (cookie[i].getName().equals(kuaidituWebAddress_2)) {
					str += URLDecoder.decode(cookie[i].getValue())+",";
				}
				if (cookie[i].getName().equals(kuaidituWebAddress_3)) {
					str += URLDecoder.decode(cookie[i].getValue())+",";
				}
			}
		}
		return str;
	}
}
